package gui;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import server.IPlayer;
import shared.PlayerPosition;
import shared.PlayerState;

import network.message.IMessage;

import client.SetbackClient;
import deck.Card;
import deck.Suit;

public class ClientObserver implements Observer{

	private TableImage table;
	private SetbackClient client;
	
	public ClientObserver(SetbackClient client) {
		this.client = client;
		client.addObserver(this);
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		if (arg0 == client) {
			IMessage message = (IMessage) arg1;
			handleMessage(message);
		}
	}

	private void handleMessage(IMessage message) {
		switch (message.getMessageType()) {
		case BID_INFO: // someone won the bid with this message
			Integer winningBid = (Integer) message.getArguements();
			break;
		case TRUMP_INFO: // the winning bidder picked this suit as trump
			Suit newTrump = (Suit) message.getArguements();
			break;
		case PLAYER_INFO: // something happened, update player state
			PlayerState playerState = (PlayerState) message.getArguements();
			table.replaceHand(playerState.getPlayerPosition(), playerState.getHand());
			table.playCard(playerState.getPlayerPosition(), playerState.getPlayedCard());
			break;
		case BID_MESSAGE: // the server is requesting we place a bid
			List<Integer> validBids = (List<Integer>) message.getArguements();
			break;
		case SUIT_MESSAGE: // the server is requesting we pick a suit (if we won the bid)
			// no arguemens, all suits are valid
			break;
		case PLAY_MESSAGE: // the server is requesting we play a card
			List<Card> validCards = (List<Card>) message.getArguements();
			table.setPlayableCards(PlayerPosition.SOUTH, validCards);
			break;
		case TRADE_MESSAGE: // the server is asking us which cards we want to trade in
			@SuppressWarnings("unchecked")
			List<Card> validTradeCards = (List<Card>) message.getArguements();
			break;
		case DEAL_MESSAGE: // the server is requesting we deal the cards
			// no arguements
			break;
		case SIT_MESSAGE: // the server is telling us a player has joined the table
			IPlayer newPlayer = (IPlayer) message.getArguements();
			break;
		}	
	}
}
